<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html><head><title>Programming - Execute Scripts</title>

<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<meta http-equiv="content-style-type" content="text/css" />
<meta http-equiv="expires" content="" />
<meta name="lang" content="en-US" />
<meta name="template-info" content="topic.htm 2012-03-19 usp" />
<meta name="creation-date" scheme="iso8601" content="2013-06-23" />
<meta name="last-modified" scheme="iso8601" content="2013-06-23" />
<meta name="publish-date" scheme="iso8601" content="" />
<meta name="version" content="1" />
<meta name="author" content="USP" />
<meta name="robots" content="follow" />
<meta name="category" content="Programming/Database/Firebird" />
<meta name="keywords" content="programming, database, Firebird, SQL, documentation." />
<meta name="description" content="" />

<link rel="stylesheet" type="text/css" href="../../../includes/topic-2.css" />
<link rel="stylesheet" type="text/css" href="../../../includes/sourcecode.css" />
<link rel="stylesheet" type="text/css" href="../local.css" />

<script language="JavaScript" type="text/javascript" src="../../../includes/global.js"></script>
<script language="JavaScript" type="text/javascript" src="../../../includes/cookies.js"></script>
<script language="JavaScript" type="text/javascript" src="../../../includes/topic.js"></script>

</head><body class="content" onload="onloadTopic()"><a name="top"></a>

<h1>Execute Scripts</h1>

<p>The Firebird DotNET Data Provider class library has a neat class: <i>FbScript</i>. It is able to parse an ISQL script file into individual commands, strip off the comments, and return each command in a separate string. Very handy. But there are some caveats: DDL and DML commands are accepted by the server, but not ISQL commands.</p>

<p>ISQL commands are generally not recognized by the database, so these must be removed, or better yet, interpreted. Such an example is</p>

<pre class="code">Set AutoDDL on;</pre>

<p>Such a statement leads to the following exception:</p>

<pre class="code">Error: Dynamic SQL Error
SQL error code = -104
Token unknown - line 1, char 5
AutoDDL
</pre>

<p>So we will ignore these types of errors in the code.</p>

<p>The current implementations looks like this:</p>

<pre class="codebox">
protected void RunScript ( )
{
    <span class="comment">// Create a script object from the input file, and parse it</span>
    <span class="comment">// into individual commands.</span>
    this.dbscript = new FirebirdSql.Data.Firebird.Isql.FbScript( this.scriptFileName );
    int result = this.dbscript.Parse( );
    Trace.WriteLine( string.Format( "Parsed {0} commands.", result ));
    <span class="comment">// We need a database connection.</span>
    this.dbcommand = this.dbconnection.CreateCommand( );
    this.dbcommand.CommandType = System.Data.CommandType.Text;
    <span class="comment">// Execute the commands.</span>
    foreach ( string cmd in this.dbscript.Results )
    {
        try
        {
            this.dbcommand.CommandText = cmd;
            result = this.dbcommand.ExecuteNonQuery( );
            Trace.WriteLine( string.Format( "Rows affected: {0}", result == -1 ? "n/a" : result.ToString() ), result == 0 ? "Error" : "Info" );
        }
        catch ( FirebirdSql.Data.Firebird.FbException ex )
        {
            Trace.WriteLine ( ExceptionFormatter.Format( ex ), "Ignored" );
            <span class="comment">// Unknown tokens ISQL commands will be ignored.</span>
            if ( ex.ErrorCode == -104 ) continue;
        }
    }
}
</pre>

<div class="footer"><hr />
<p>Page author: USP &bull; Page editor: USP &bull; Last update: 2013-06-06 &bull; 1</p>
<p>&copy; 2013 Synesis Ulrich Sprick. All rights reserved. See <a href="../../../copyright.htm">Copyright details</a>.</p>
<p><a href="#top">Back to Top</a> | <a href="../../../index.htm" target="_top">Home</a> | <a id="reload" onclick="reloadFrameset()" href ="../../../index.htm" target="_top" >Reload Frameset</a></p>
</div></body></html>
